<template>
	<view>
		<view class="container">
			<cu-custom bgColor="bg-gradual-orange" :isBack="true">
				<block slot="content">我的关注</block>
			</cu-custom>

			<!-- tab选择 -->
			<view class="tab-container">
				<view :class="['tab-item', { 'tab-item-active': selectTabIndex === index }]"
					v-for="(tab, index) in tabOptions" :key="index" @click="selectTab(index)">
					{{ tab }}
				</view>
			</view>

			<!-- 商品 -->
			<view class="product" v-if="selectTabIndex === 0">
				<view class="product-item" v-for="(product, index) in productData" :key="index"
				@tap="jumpTo('/pages/goods/details?id=' + product.id)">
					<image class="product-image" :src="product.image" mode=""></image>
					<view class="product-info">
						<view class="item-info-title">
							{{ product.title }}
						</view>
						<view class="item-info-price">
							<text>市场价</text>
							<text class="price">￥{{ product.price }}</text>
						</view>
						<view class="item-info-merchant">
							<image src="../../static/share.gif" mode=""></image>
							<view class="">
								付加加
							</view>
						</view>
					</view>
				</view>
			</view>

			<!-- 商家 -->
			<view class="merchant" v-if="selectTabIndex === 1">
				<view class="businesses-item" v-for="(merchant, index) in merchantData" :key="index"
					@tap="jumpTo('/pages/home/detail?id=' + merchant.id)">
					<image class="item-left-image" :src="merchant.logo" mode=""></image>
					<view class="item-center-text">
						<view class="item-title">
							{{ merchant.title }}
						</view>
						<view class="item-phone">
							<image src="https://app.yycjkb.cn/index/fujiajia/phone.png" mode=""></image>
							{{ merchant.mobile }}
						</view>
					</view>
					<view class="item-right-icon">
						<image class="icon-red" src="https://app.yycjkb.cn/index/fujiajia/red.png" mode="aspectFill">
						</image>
						<view class="address">
							<image class="icon-address" src="https://app.yycjkb.cn/index/fujiajia/定位.png" mode="aspectFill">
							</image>
							{{ merchant.address }}
						</view>
					</view>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	import user from "@/api/user";

	export default {
		data() {
			return {
				tabOptions: ["商品", "商家"],
				selectTabIndex: 0, // 默认选中商品
				productData: [], // 关注的商品列表
				merchantData: [],
				IMAGE_BASE_URL: "https://cos.yycjkb.cn" // 图片 CDN 地址
			};
		},
		async created() {
			await this.loadProductData(); // 加载商品收藏数据
			await this.loadMerchantData(); // 加载商家收藏数据
		},
		methods: {
			jumpTo(url) {
				uni.navigateTo({ url });
			},
			// jumpTab(url) {
			// 	console.log(url);
   //      uni.navigateTo({ url }); // 将 switchTab 改为 navigateTo
   //  },
			// 获取关注的商品列表
			async loadProductData() {
				try {
					const res = await user.followProductList();
					if (res.code === 1) {
						this.productData = res.data.list.map(item => ({
							id: item.product_id,
							image: item.thumb ? this.IMAGE_BASE_URL + item.thumb : "/static/default.png",
							title: item.name || "未知商品",
							infoText: "已关注",
							price: item.price || "暂无报价",
							merchantImage: "/static/default.png", // 如果 API 返回商家信息，可替换
							merchantName: "商家信息待补充"
						}));
					} else {
						this.$toast(res.msg);
					}
				} catch (error) {
					console.error("获取关注商品失败:", error);
				}
			},
			// 获取关注的商家列表
			async loadMerchantData() {
				try {
					const res = await user.followShopList();
					if (res.code === 1) {
						this.merchantData = res.data.list;
					} else {
						this.$toast(res.msg);
					}
				} catch (error) {
					console.error("获取关注商家失败:", error);
				}
			},
			selectTab(index) {
				this.selectTabIndex = index;
				if (index === 0) {
					this.loadProductData();
				} else {
					this.loadMerchantData();
				}
			}
		}
	};
</script>


<style lang="less" scoped>
	.tab-container {
		display: flex;
		justify-content: space-around;
		// padding: 20rpx 0;
		background-color: #fff;

		.tab-item {
			font-size: 30rpx;
			padding: 30rpx 20rpx;
			width: 50%;
			height: 100%;
			cursor: pointer;
			position: relative;
			text-align: center;

			&.tab-item-active {
				color: #f76e00;

				&::before {
					content: "";
					position: absolute;
					left: 0;
					bottom: 0%;
					width: 100%;
					height: 6rpx;
					background-color: #ffd1aa;
				}
			}
		}
	}

	.merchant {
		.businesses-item {
			width: 100%;
			height: 204rpx;
			background-color: #fff;
			border-radius: 26rpx;
			margin-top: 20rpx;
			padding: 20rpx;
			display: flex;

			.item-left-image {
				width: 169rpx;
				height: 169rpx;
				border-radius: 26rpx;
			}

			.item-center-text {
				padding: 0 20rpx;
				display: flex;
				flex-direction: column;
				justify-content: space-between;

				.item-phone {
					display: flex;
					align-items: center;
					color: #7b7b7b;
					padding-top: 14rpx;

					image {
						width: 23rpx;
						height: 23rpx;
					}
				}

				.item-buy {
					display: flex;
					align-items: center;

					.money-box {
						width: 44rpx;
						height: 44rpx;
						background-color: #ff9b45;
						display: flex;
						justify-content: center;
						align-items: center;
						border-bottom-left-radius: 6rpx;
						border-top-left-radius: 6rpx;

						image {
							width: 29rpx;
							height: 29rpx;
						}
					}

					.money-data {
						height: 44rpx;
						width: 155rpx;
						text-align: center;
						border-bottom-right-radius: 6rpx;
						border-top-right-radius: 6rpx;
						color: #ff9b45;
						background-color: rgba(255, 155, 69, 0.2);
					}
				}
			}

			.item-right-icon {
				margin-left: auto;
				display: flex;
				flex-direction: column;
				justify-content: space-between;

				.icon-red {
					width: 40rpx;
					height: 90rpx;
					margin-left: auto;
					margin-top: -10rpx;
				}

				.address {
					display: flex;
					align-items: center;
					color: #8a8a8a;

					image {
						height: 44rpx;
						width: 44rpx;
					}
				}
			}
		}
	}

	.product {
		padding-top: 20rpx;
		display: grid;
		grid-template-columns: repeat(auto-fit, minmax(350rpx, 1fr));
		grid-gap: 20rpx;
		justify-items: center;

		.product-item {
			border-radius: 20rpx;
			overflow: hidden;
			width: 350rpx;
			height: 515rpx;
			background-color: #fff;

			.product-image {
				width: 100%;
				height: 350rpx;
			}

			.product-info {
				padding: 10rpx 20rpx;

				.item-info-title {
					white-space: nowrap;
					overflow: hidden;
					text-overflow: ellipsis;
				}

				.item-info-price {
					color: #7b7b7b;
					font-size: 26rpx;
					padding: 10rpx 0;

					.price {
						margin-left: 10rpx;
						color: #f76e00;
					}
				}

				.item-info-merchant {
					display: flex;
					align-items: center;
					font-size: 26rpx;
					color: #7b7b7b;

					image {
						width: 35rpx;
						height: 35rpx;
						border-radius: 100%;
						vertical-align: middle;
						margin-right: 10rpx;
					}
				}
			}
		}
	}
</style>